home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / dev / e / icongad.lha / icongadgets.e < prev   
Text File  |  1997-10-25  |  8KB  |  239 lines

  1. ;/*
  2.     
  3.     ec icongadgets.e
  4.     run icongadgets
  5.     quit
  6.     
  7.     An easy way to have a nice button bank using icons. Will Harwood.
  8.  
  9.     Hello! I have been trying for ages to find a simple way of getting a bank
  10.     of image buttons for any programs I might feel like writing. I searched in vain for
  11.     a gadtools command or tag, but then I downloaded a tiny bit of code by Marco 
  12.     Talamelli called ShowIcon and I thought YES! My search is at an end! Once it
  13.     actually worked (see Bugs) I thought to myself, "if only I had found this on the
  14.     Aminet, how much easier my life would have been...", so here it is.
  15.  
  16.     It also shows using easyrequest, standard intuimessage reading techniques, setting
  17.     the busy pointer on v39+ machines, changing window titles... Beginners: mess about
  18.     with this code as much as possible to learn how it works, because there is no better 
  19.     way of learning a language than crashing your machine.
  20.         
  21.     Since this file was the result of about half an hours typing and a free download
  22.     from the Aminet (isn't Swansea uni nice?), I can hardly start going on about copyrights
  23.     and all that shit, but I will say that if anyone finds this ditty useful then I
  24.     would appreciate an E-mail to lift my spirits in these dark student days of bank loans
  25.     and getting rat-arsed every night (oh it's such a drag ;). And there has to be some-
  26.     one else at Swansea who programs in E, yes? hello? You are not alone?
  27.  
  28.     The source is pretty self explanatory (and like I know what I'm talking about...), and
  29.     can be dropped almost verbatim into what ever program you chose to write. 
  30.  
  31.     Requires:
  32.     
  33.         WB 3.0
  34.         Amiga E (I compiled this on 3.2e, but it should work on anything =>3.0)
  35.  
  36.     To compile and run:
  37.     
  38.         Open a shell window and type icongadgets.e
  39.  
  40.     Author:
  41.     
  42.         I am ('97 - '00) studying Computer Science at Swansea University, and I would like
  43.         to hear from anyone who is interested in [Amiga] E, or indeed anything from Britpop
  44.         to HIGNFY (I hate loading Pegasus to find that I have no E-mails...). I am espec-
  45.         ially interested in hearing from anyone who can tell me a neat way of loading the
  46.         icon images into a bitmap (i.e. not grabbing the gfx straight off the window), or
  47.         doing the same with datatypes?
  48.         
  49.         (this address is okay until June '98)
  50.         
  51.             Will Harwood
  52.             134 Hendrefoelan Student Village,
  53.             Sketty,
  54.             Swansea,
  55.             SA2 7QG
  56.             UK
  57.  
  58.         or...
  59.         
  60.             Will Harwood
  61.             618 Dorchester Road,
  62.             Weymouth,
  63.             Dorset.
  64.             DT3 5LH
  65.             UK
  66.         
  67.         E-mail:        
  68.     
  69.         I am but a number...
  70.     
  71.             147800.97@swansea.ac.uk
  72.     
  73.     Bugs:
  74.     
  75.         Just the one, try changing the starting index for objects[] to 0 like it's 
  76.         supposed to be (i.e. change max=1 to max=0 and FOR n:=1 to FOR n:=0) and then 
  77.         watch as it crashes when it Free'sDiskObject. WHY?
  78.         
  79.     Problems:
  80.     
  81.         I suppose it's a fairly inefficient method in terms of disk space and loading time,
  82.         but you can't have everything.
  83.         
  84.  */
  85.  
  86.  
  87. MODULE    'workbench/workbench',
  88.             'exec/ports',
  89.             'icon',
  90.             'intuition/screens', 'intuition/intuition',
  91.             'gadtools', 'libraries/gadtools'
  92.  
  93.  
  94. CONST MAX_BUTTONS=20, QUIT=21, ABOUT=22        /* gadtools id's should be >MAX_BUTTONS or you
  95.                                                                 could end up with two buttons having the same
  96.                                                                 id */
  97.  
  98.  
  99. PROC main() HANDLE
  100.     DEF win=NIL:PTR TO window, glist=NIL, objects[MAX_BUTTONS]:ARRAY OF diskobject, max=1, n,
  101.          screen=NIL:PTR TO screen, vi=NIL, gad, x=5, h, w
  102.     
  103.     /* Open libraries */
  104.     IF NIL=(gadtoolsbase:=OpenLibrary('gadtools.library', 39)) THEN 
  105.         Throw("lib", 'main: Could not open gadtools library')
  106.     IF NIL=(iconbase:=OpenLibrary('icon.library', 0)) THEN
  107.         Throw("lib", 'main: Could not open the icon library')
  108.     
  109.     /* grab the public screen and get some visual info */
  110.     IF NIL=(screen:=LockPubScreen(NIL)) THEN Throw("scr", 'main: Could not lock the public screen')
  111.     IF NIL=(vi:=GetVisualInfoA(screen, NIL)) THEN Throw("vi", 'main: Could not getvisualinfo')
  112.  
  113.     /* create gadget list */
  114.     IF NIL=(gad:=CreateContext({glist})) THEN Throw("gad", 'main: Could not create context')
  115.  
  116.     /* add some gadtools gadgets */
  117.     gad:=CreateGadgetA(BUTTON_KIND, gad,
  118.                     [5, 69,
  119.                      100, 11,
  120.                      'Quit', NIL,
  121.                      QUIT, 0,
  122.                      vi, NIL]:newgadget,
  123.                     [NIL])
  124.  
  125.     gad:=CreateGadgetA(BUTTON_KIND, gad,
  126.                     [295, 69,
  127.                      100, 11,
  128.                      'About', NIL,
  129.                      ABOUT, 0,
  130.                      vi, NIL]:newgadget,
  131.                     [NIL])
  132.     
  133.     
  134.     IF NIL=(win:=OpenWindowTagList(0, 
  135.                 [WA_INNERWIDTH, 400, WA_INNERHEIGHT, 80,
  136.                  WA_FLAGS, WFLG_GIMMEZEROZERO OR WFLG_DRAGBAR OR WFLG_DEPTHGADGET OR WFLG_CLOSEGADGET,
  137.                  WA_IDCMP, IDCMP_CLOSEWINDOW OR IDCMP_GADGETUP OR BUTTONIDCMP OR IDCMP_REFRESHWINDOW,
  138.                  WA_GADGETS, glist,
  139.                  WA_TITLE, 'Icons as gadgets example by Will Harwood',
  140.                  WA_SCREENTITLE, 'Will Harwood, 147800.97@swansea.ac.uk',
  141.                  WA_PUBSCREEN, screen,
  142.                  NIL])) THEN Throw("win", 'main: Could not open window')
  143.  
  144.      /* add some buttons: Change the names to any icons (minus .info) you like. */
  145.     w, h:=addiconbutton(win, objects[max], 'Workbench:prefs/ScreenMode', max++, x, 0)
  146.     w, h:=addiconbutton(win, objects[max], 'Workbench:prefs/Font', max++, x:=x+w, 0)
  147.     w, h:=addiconbutton(win, objects[max], 'Workbench:prefs/WBPattern', max++, x:=x+w, 0)
  148.     w, h:=addiconbutton(win, objects[max], 'Workbench:prefs/Time', max++, x:=x+w, 0)
  149.     w, h:=addiconbutton(win, objects[max], 'Workbench:prefs/Locale', max++, x:=x+w, 0)
  150.     w, h:=addiconbutton(win, objects[max], 'Workbench:prefs/Input', max++, x:=x+w, 0)
  151.  
  152.     /* redraw everything */
  153.     RefreshWindowFrame(win)
  154.     
  155.     REPEAT
  156.     UNTIL handle(win)
  157.  
  158.  
  159. EXCEPT DO
  160.  
  161.     IF exception THEN PrintF('\h \s\n', exception, exceptioninfo)
  162.  
  163.     /* close gui */
  164.     IF win THEN CloseWindow(win)
  165.     IF glist THEN FreeGadgets(glist)
  166.     IF vi THEN FreeVisualInfo(vi)
  167.     IF screen THEN UnlockPubScreen(NIL, screen)
  168.     
  169.     /* free buttons */
  170.     FOR n:=1 TO max-1
  171.         IF objects[n] THEN FreeDiskObject(objects[n])
  172.     ENDFOR
  173.     
  174.     /* close libraries */
  175.     IF gadtoolsbase THEN CloseLibrary(gadtoolsbase)
  176.     IF iconbase THEN CloseLibrary(iconbase)
  177. ENDPROC
  178.  
  179. PROC addiconbutton(win:PTR TO window, object:PTR TO diskobject, iconname, n, x, y)
  180.     IF NIL=(object:=GetDiskObject(iconname)) THEN Throw("icon", 'addiconbutton: Could not GetGiskObject')
  181.     /* change gadget attributes here. I *think* it's safe to do this, at least it doesn't
  182.         crash... */
  183.     object.gadget.leftedge:=x
  184.     object.gadget.topedge:=y
  185.     object.gadget.gadgetid:=n
  186.     AddGadget(win, object.gadget, NIL)
  187.     /* pass back the width and height so we can fit the gadgets in properly */
  188. ENDPROC object.gadget.width, object.gadget.height
  189.  
  190. PROC handle(win:PTR TO window)
  191.     DEF msg:PTR TO intuimessage, class, code, gadget:PTR TO gadget, quit=0, id, str[200]:STRING,
  192.          signals
  193.     
  194.     signals:=Wait(Shl(1, win.userport.sigbit))
  195.     
  196.     IF Shl(1, win.userport.sigbit) AND signals
  197.         WHILE msg:=Gt_GetIMsg(win.userport)
  198.             class:=msg.class
  199.             code:=msg.code
  200.             gadget:=msg.iaddress
  201.             id:=gadget.gadgetid
  202.             Gt_ReplyIMsg(msg)
  203.             
  204.             SELECT class
  205.            CASE IDCMP_REFRESHWINDOW
  206.              Gt_BeginRefresh(win)
  207.                Gt_EndRefresh(win, TRUE)
  208.             CASE IDCMP_CLOSEWINDOW
  209.                 quit:=TRUE
  210.             CASE IDCMP_GADGETUP
  211.                 IF id=QUIT
  212.                     quit:=TRUE
  213.                 ELSEIF id=ABOUT
  214.                     /* put up a busy pointer with a slight delay (to see why comment the delay
  215.                         tags) */
  216.                     SetWindowPointerA(win, [WA_BUSYPOINTER, TRUE, WA_POINTERDELAY, TRUE, NIL])
  217.                     SetWindowTitles(win, '-- About --', -1)
  218.                     EasyRequestArgs(0, 
  219.                         [SIZEOF easystruct, 0, 
  220.                          'icongadgets', 
  221.                          'A little example of how you CAN EASILY have\ngadtools image buttons\n\nWill Harwood, 147800.97@swansea.ac.uk', 
  222.                          'Yep'], 0, NIL)    
  223.                     /* reset the pointer back to normal */
  224.                     SetWindowPointerA(win, [WA_BUSYPOINTER, FALSE, NIL])
  225.                     SetWindowTitles(win, 'Icons as gadgets example by Will Harwood', -1)
  226.                 ELSE
  227.                     /* better than printf */
  228.                     StringF(str, 'icon gadget \d clicked', id)
  229.                     SetWindowTitles(win, str, -1)
  230.                 ENDIF
  231.             CASE IDCMP_VANILLAKEY
  232.                 SELECT 256 OF code
  233.                 CASE 27, "q", "Q", "å"        /* this should be every variety of quit there is */
  234.                     quit:=TRUE
  235.                 ENDSELECT
  236.             ENDSELECT
  237.         ENDWHILE
  238.     ENDIF
  239. ENDPROC quit